home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacPeople 2003 February 1
/
MACPEOPLE-2003-02-01.ISO.7z
/
MACPEOPLE-2003-02-01.ISO
/
ぶらりオンラインウェアの旅
/
おしゃべり漂流記
/
xGates
/
xGates 1.2 Source Code.sit
/
xGates 1.2 Source Code
/
util.c
< prev
next >
Wrap
Text File
|
2002-12-08
|
6KB
|
186 lines
/*
xGates -- Stunningly entertaining action game for MacOS Classic / MacOS X
Copyright (C) 2002 Sveinbjorn Thordarson <paladeen@soth.zoneit.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
util.c - a collection of generally useful functions
*/
#include "externs.h"
#include "prototypes.h"
#include "definitions.h"
//////////////////////////////////////////////////////
//fast, efficient function to copy pascal strings
//courtesy of Michael Gleason
//mgleason@cse.unl.edu
//////////////////////////////////////////////////////
unsigned char *pStrcpy(register unsigned char *dst, register unsigned char *src)
{
#define word short
#define IsWordAligned(a) (((word) (a) & (word)(sizeof(word) - 1)) == 0)
register short nLWords;
register short len = src[0] + 1;
unsigned char *s = dst;
if (IsWordAligned(dst) && IsWordAligned(src))
{
nLWords = len / (short)sizeof(long);
/* This is the number of single bytes to copy afterward,
* which is len (mod 4). We can do powers of 2 with &. */
len &= ((short)(sizeof(long) - 1));
for ( ; nLWords > 0; --nLWords) {
/* I'd like to be able to do this as an autoincrement
* statement, but C won't let me. I want to stay
* away from 68000 gazzembly now that PowerPC is here,
* otherwise I'd just stick in an asm { move.l (src)+, (dst)+ }
* here. Actually I timed that too, and it's slower!
*/
*(long *)dst = *(long *)src;
dst += sizeof(long);
src += sizeof(long);
}
for (; len > 0; --len ) {
*dst++ = *src++;
}
} else for (; --len >= 0; ) {
/* Just copy it the usual way, byte by byte. In the 68000's
* case, it's unlikely we'll ever reach this point since
* p-strings are almost always word-aligned anyway.
*/
*dst++ = *src++;
}
return(s);
} /* pStrcpy */
//////////////////////////////////////////////////////
//smarter than events. Checks keymap to see if a
//designated key is down
//courtesy of Thorlindur Thorolfsson, =)
//////////////////////////////////////////////////////
Boolean CheckKeyMapForKey(KeyMap theKeyMap, short theKey)
{
short byteIndex;
char theByte, theBit;
char *thePointer;
byteIndex = theKey >> 3;
thePointer = (char *)&theKeyMap[0];
theByte = *(char *)(thePointer + byteIndex);
theBit = 1L<<(theKey & 7);
return( (theByte & theBit) != 0 );
}
//////////////////////////////////////////////////////
//get a random between two numbers
//////////////////////////////////////////////////////
short RandomBetween(short value1, short value2)
{
short temp;
temp = value2 - value1 + 1;
if(temp == 0)/*Justin Case*/
temp = 1;
temp = (Random() & 0x7fff) % temp;
return temp + value1;
}
//////////////////////////////////////////////////////
//get absolute value of number
//////////////////////////////////////////////////////
short absolute (short value)
{
if (value < 0)
{
return(value * -1);
}
return(value);
}
//////////////////////////////////////////////////////
//use Quickdraw pen modes to darken a designated area
//////////////////////////////////////////////////////
void ShadeRect (Rect *rect)
{
RGBForeColor(&myGrayColor);
PenMode(subPin);
PaintRect(rect);
ForeColor(blackColor);
PenNormal();
RGBForeColor(&myBlackColor);
}
//////////////////////////////////////////////////////
//do nothing for a designated number of ticks
//////////////////////////////////////////////////////
void Sleep (short ticks)
{
long count = TickCount();
while (TickCount() < count+(ticks)) { }
}
//////////////////////////////////////////////////////
//place a rect in the center of another rect
//////////////////////////////////////////////////////
Rect* CenterRectInRect(Rect *smallRect, Rect *bigRect)
{
Rect newRect;
newRect.left = (bigRect->right - smallRect->right) / 2;
newRect.right = ((bigRect->right - smallRect->right) / 2) + smallRect->right;
newRect.top = (bigRect->bottom - smallRect->bottom) / 2;
newRect.bottom = ((bigRect->bottom - smallRect->bottom) / 2) + smallRect->bottom;
return (&newRect);
}
//////////////////////////////////////////////////////
//Red Alert dialog
//////////////////////////////////////////////////////
void DoAlert(Str255 errorString, Str255 expStr)
{
StandardAlert (kAlertStopAlert, errorString, expStr, NULL, NULL);
}
//////////////////////////////////////////////////////
//Red Alert dialog and then quit app
//////////////////////////////////////////////////////
void FatalErrorAlert(Str255 errorString, Str255 expStr)
{
StandardAlert (kAlertStopAlert, errorString, expStr, NULL, NULL);
QuitApp();
}